/**
 * @author Luc Yriarte
 * 
 * Licence : BSD http://www.opensource.org/licenses/bsd-license.php
 *  
 */
package basicSim;

/**
 * Vertical Cylinder, height h on the y axis, disks of radius r approximated
 * as polygons of n sides.
 */

public class PolyCylinder extends Object3D {

	double height, radius;

	int n_sides;

	/**
	 * @param nTotal segments for base circle approximation
	 * @param n segments to draw a partial base
	 * @param h height on the y axis
	 * @param r base radius
	 */
	public void init(int nTotal, int n, double h, double r) {
System.out.println("PolyCylinder "+ nTotal + " " + n + " " + h + " " + r + " ");
		height = h;
		radius = r;
		if (n < 3)
			n = 3;
		n_sides = n;
		nFacet = n_sides + 2;
		facet = new PolygonEdgeIndexes[nFacet];
		// bottom polygon
		facet[n_sides] = new PolygonEdgeIndexes(this, n_sides);
		// top polygon
		facet[n_sides + 1] = new PolygonEdgeIndexes(this, n_sides);
		double dh = h / 2;
		double step = 2 * Math.PI / nTotal;
		Vector3D v[] = new Vector3D[n * 2];
		int[][] e = new int[n * 3][2];
		int i;
		for (i = 0; i < n; i++) {
			// bottom polygon
			v[i] = new Vector3D(r * Math.cos(i * step), -dh, r
					* Math.sin(i * step));
			e[i][0] = i;
			e[i][1] = (i + 1) % n;
			// bottom polygon facet
			facet[n_sides].edge[i] = i;
			// top polygon
			v[i + n] = new Vector3D(r * Math.cos(i * step), dh, r
					* Math.sin(i * step));
			e[i + n][0] = i + n;
			e[i + n][1] = ((i + 1) % n) + n;
			// top polygon facet
			facet[n_sides + 1].edge[i] = i + n;
			// vertical lines
			e[i + n * 2][0] = i;
			e[i + n * 2][1] = i + n;
			// vertical rectangle facets
			facet[i] = new PolygonEdgeIndexes(this, 4);
			facet[i].edge[0] = i;
			facet[i].edge[1] = (i + 1) % n;
			facet[i].edge[2] = ((i + 1) % n) + n;
			facet[i].edge[3] = i + n;
		}
		mesh = new Mesh(n * 2, n * 3, v, e);
	}

	public PolyCylinder(int nTotal, int n, double h, double r) {
		super();
		init(nTotal, n, h, r);
	}

	public PolyCylinder(int n, double h, double r) {
		super();
		init(n, n, h, r);
	}

	public PolyCylinder(double h, double r) {
		super();
		init(N_DEFAULT, N_DEFAULT, h, r);
	}

	public String strObject(int nTab) {
		String str = "", strTab = "";
		for (int i = 0; i < nTab; i++)
			strTab += "\t";
		str += strTab + "{ cylinder r=" + new Double(radius).toString() + " h="
				+ new Double(height).toString();
		if (n_sides != N_DEFAULT)
			str += " n=" + new Integer(n_sides).toString();
		str += "\n";
		str += strCoord(nTab);
		str += strColor(nTab);
		str += strChild(nTab);
		str += strTab + "}\n";
		return str;
	}

}
